$(function () {
    /**初始化人员选择**/
    init_selectReceUser();
    /**初始化时间选择**/
    $("#select_time span").html(moment().subtract("days",29).format("YYYY-MM-DD")+" - "+moment().format("YYYY-MM-DD"));
    $('#select_time').daterangepicker({
        timePicker: true, //显示时间
        timePicker24Hour: true, //时间制
        timePickerSeconds: true, //时间显示到秒
        startDate: moment().hours(0).minutes(0).seconds(0), //设置开始日期
        endDate: moment(new Date()), //设置结束器日期
        maxDate: moment(new Date()), //设置最大日期
        "opens": "center",
        ranges: {
            '今天': [moment(), moment()],'昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            '上周': [moment().subtract(6, 'days'), moment()],
            '前30天': [moment().subtract(29, 'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        showWeekNumbers: true,
        locale: {
            format: "YYYY-MM-DD HH:mm:ss", //设置显示格式
            applyLabel: '确定', //确定按钮文本
            cancelLabel: '取消', //取消按钮文本
            customRangeLabel: '自定义',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'
            ],
            firstDay: 1
        },
    }).on('apply.daterangepicker', function(ev, picker) {
        var starttime = picker.startDate.format('YYYY-MM-DD');
        var endtime = picker.endDate.format('YYYY-MM-DD');
        $("#select_time span").html(starttime+" - "+endtime);
        $("#starttime").val(starttime);
        $("#endtime").val(endtime);
        $JuXue.refreshTable("saleCustReceTable");
    });
    /**列表查询**/
    var settings = {
        url: ctx + "cusDynamic/getAllbigEmpFemData",
        pageList: [10,50, 100,300,500],
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                dyCreator   :  $("#geren").val(),   //跟进人
                dySourceType :$("#ResType").val(),  // 资源类型
                starttime : $("#starttime").val(), //开始时间
                endtime   : $("#endtime").val()    //结束时间
            };
        },
        columns: [{
            checkbox: true,
        }, {
            field: 'dyDisName',
            title: '客户名称',
            clickToSelect:false,
            formatter:function (value, row, index) {return "<a href='javascript:void(0)' onclick='viewCusReceDetail(this);' type='"+row.dySourceType+"' uuid = '"+row.dyCusUuid+"'>"+value+"</a>";}
        }, {
            field: 'dySourceType',
            title: '资源类型',
            formatter:function (value, row, index) {
                if(value=="1"){typename = "推广资源类";
                }else if(value=="2"){typename = "网校资源类";
                }else if(value=="3"){typename = "大资源类";
                }else if(value=="5"){typename = "AI资源类";
                }else{typename = "个人录入";}
                return typename;
            }
        }, {
            field: 'dyCreatorName',
            title: '分配人',
        },{
            field:'dyZhuanName',
            title: '接收人'
        }, {
            field: 'dyCreattime',
            title: '创建时间',
        }]
    };
    $JuXue.initTable('saleCustReceTable', settings);
});
/**导出领取客户资料**/
function exportDynamicInfo() {
    var ids = $("#saleCustReceTable").bootstrapTable("getSelections");
    if (!ids.length) {$JuXue.n_warning("请勾选需要导出的领取客户信息！");return;}
    var ids_arr = new Array();
    var stype = new Array();
    for (var i = 0; i < ids.length; i++) {
        ids_arr.push(ids[i].dyCusUuid) ;
        stype.push(ids[i].dySourceType);
    }
    if (ids_arr.length <= 0 || stype.length <=0){$JuXue.n_warning("请勾选需要导出的领取客户信息！");return;}

    var uid = ids_arr.join(",");
    var strtype = stype.join(",");
    $.post(ctx + "myCustomer/exportDetailforFenPeiExcel", {typeids:strtype,"keyids": uid}, function (r) {
    if (r.code === 0) {window.open("file/download?fileName=" + r.msg + "&delete=" + true);}
    else {$JuXue.n_warning(r.msg);}});
}
/**
 * 切换资源类型
 * **/
function addCenterMethod(btn) {
    $("#customeTrack").find("button").each(function () {$(this).removeClass("btn-primary");});
    $(btn).addClass("btn-primary");
    var type = $(btn).attr("data-click");
    $("#ResType").val(type);
    //动态加载
    $JuXue.refreshTable("saleCustReceTable");
}

/**切换时间类型**/
function selectReceSeletTimne(btn) {
    var data_cliak = $(btn).attr("data-click");
    if(data_cliak === "selft"){
        $("#select_time").removeClass("hidden");
        $("#starttime").val(moment().subtract("days",29).format("YYYY-MM-DD"));
        $("#endtime").val(moment().format("YYYY-MM-DD"));
    }else{
        $("#select_time").addClass("hidden");
        $("#starttime").val('')
        $("#endtime").val('');
    }
    $("#timeRemove").find("button").each(function () {$(this).removeClass("btn-primary");});
    $(btn).addClass("btn-primary");
    $JuXue.refreshTable('saleCustReceTable');
}
/**初始化选择人员**/
function init_selectReceUser(){
    $.post(ctx+"dept/getUserAndDeptByTreeWhisRole",{},function (r) {
        var array  = r.msg;
        $("#select_Rece_user").jstree({
            "core": {
                "multiple": true,//单选
                "data":array, "icon":false,
                "themes":{"icons":false}
            },
            "checkbox" : {"keep_selected_style" : false},
            "plugins" : [ "wholerow", "checkbox" ]
        });
        // 获得点击节点的id
        $('#select_Rece_user').on("changed.jstree", function (e, data) {
            var nodeid = data.node.id;
            var text = data.node.text;
            var action = data.action;;
            if (action == "deselect_node") {
                $("#otherUserSelect").find("div").each(function () {var data_id = $(this).attr("data-id");if (data_id == nodeid) $(this).remove();});
            }else{
                /**判断是不是超过 3个 超过3个则不能继续追加**/
                var divlength = $("#otherUserSelect").find("div");
                if(divlength.length >= 3){$JuXue.n_danger("选择查询类型不能超过3个");$("#select_Rece_user").jstree("deselect_node",nodeid);return;
                }else{
                    if(divlength.length >0){
                        var ble = false;
                        $("#otherUserSelect").find("div").each(function () {var data_id = $(this).attr("data-id");if (data_id == nodeid){ble = true;}});
                        if(!ble){var label = "<div class='label label-danger p-3 m-3 ' data-id='"+nodeid+"'>"+text+"</div>";$("#otherUserSelect").append(label);}
                    }else{var label = "<div class='label label-danger p-3 m-3 ' data-id='"+nodeid+"'>"+text+"</div>";$("#otherUserSelect").append(label);}
                }
            }
        });
    });
}
/**确定人员选择**/
function selectReceUserList(){
    $('#selectReceUsers').removeClass('open');
    var allUser = new Array();
    $("#otherUserSelect").find("div").each(function () {var ids  = $(this).attr("data-id");allUser.push(ids);});
    var users = allUser.join(",");$("#geren").val(users);
    $JuXue.refreshTable("saleCustReceTable");
}
/**擦看详情**/
function viewCusReceDetail(btn){
    var type = $(btn).attr("type");
    var uuid = $(btn).attr("uuid");$("#customer_uuid").val(uuid);$("#customer_ids").val(uuid);
    $.post(ctx+"myCustomer/loadCustomerDetail",{"type":type},function (r) {$("#myCustomer-modal").empty().append(r).modal('show');});
}
